package org.idea.spring.framework.http.starter;

import lombok.Getter;
import lombok.Setter;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 * @Author linhao
 * @Date created in 6:18 下午 2021/4/30
 */
@Getter
@Setter
public class SpringHttpClient {

    private String url;

    private String charsetName;

    public String getHtml() {
        try {
            if (url == null) {
                return null;
            }
            URL url = new URL(this.url);
            URLConnection urlConnection = url.openConnection();
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), charsetName));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
